package crc

import (
	"hash/crc32"
	"hash/crc64"
	"io"
	"os"
)

func GetCrc32(data []byte) uint32 {
	table := crc32.MakeTable(crc32.IEEE)
	return crc32.Checksum(data, table)
}

func GetCrc64(data []byte) uint64 {
	table := crc64.MakeTable(crc64.ISO)
	return crc64.Checksum(data, table)
}

func GetCrc32ByFile(f *os.File) (uint32, error) {
	hash := crc32.NewIEEE()
	if _, err := io.Copy(hash, f); err != nil {
		return 0, err
	}
	return hash.Sum32(), nil
}

func GetCrc32ByPath(path string) (uint32, error) {
	f, err := os.Open(path)
	if err != nil {
		return 0, err
	}
	hash := crc32.NewIEEE()
	if _, err = io.Copy(hash, f); err != nil {
		return 0, err
	}
	return hash.Sum32(), nil
}
